
function setCookie(name, value, day) {
    var date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires=' + date;
}
function getCookie(name) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split('; ');
    for (var i = 0; i < arrCookie.length; i++) {
        var arr = arrCookie[i].split('=');
        if (arr[0] == name) {
            return arr[1];
        }
    }
    return '';
}

function at(p, a) {
    location.assign(((o, q) => Object.keys(o).reduce((p, k) => o[k] === null ? p : `${p},${k}:${o[k]}`, q))
        (...typeof (p) === "string" ? [{}, p] : [
            Object.assign(Object.fromEntries(Array.from(location.search.matchAll(/\,(\w+)(?:\:([\%\+\-\.\/\=\w]*))?/g)).map(v => v.slice(1))), p),
            a || location.search.replace(/\,.+/, "")]))
}

function url64_encode(data)
{
	const
	key = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-', 
	bin = unescape(encodeURIComponent(data));
	let buffer = '';
	for (let i = 0, length = bin.length; i < length;)
	{
		let value = bin.charCodeAt(i++) << 16;
		buffer += key.charAt(value >> 18 & 63);
		if (i < length)
		{
			value |= bin.charCodeAt(i++) << 8;
			buffer += key.charAt(value >> 12 & 63);
			if (i < length)
			{
				value |= bin.charCodeAt(i++);
				buffer += key.charAt(value >> 6 & 63);
				buffer += key.charAt(value & 63);
				continue;
			}
			buffer += key.charAt(value >> 6 & 63);
			break;
		}
		buffer += key.charAt(value >> 12 & 63);
		break;
	}
	return buffer;
}

document.addEventListener("DOMContentLoaded", function () {
    const containerNode = document.querySelector("div.container");
    if (containerNode != null) {
        document.querySelector('div.notice>span.menu').addEventListener('click', () => {
            containerNode.classList.toggle("iconslide");
            if (containerNode.classList.contains("iconslide")) {
                setCookie("aside", "hide", 30);
            } else {
                setCookie("aside", "show", 30);
            }
        });
    }

    /*<div class="webapp-bar merge"><span>已选中 <font color="#ff0000">0</font>项</span><button class="tbbtn">批量删除</button><form method="post" action="?admin/user-delete"><input type="hidden" name="ids" value=""><select name="group_id"><option value="">选择转移的目标分组</option><option value="4">fasd</option><option value="3">asdf</option><option value="2">asdf</option><option value="1">fasdf</option></select></form></div>*/
    const selectAllNode = document.querySelector("table[class|=webapp]>tbody>tr.fieldset>td.selectall");
    const checkboxNodes = document.querySelectorAll("table[class|=webapp]>tbody>tr>td>input[type=checkbox]");
    if (selectAllNode != null && checkboxNodes != null) {
        checkboxNodes.forEach((node) => {
            node.addEventListener('click', () => {
                node.parentNode.parentNode.classList.toggle('selected');
                let count = 0;
                for (let i = 0; i < checkboxNodes.length; i++) {
                    if (checkboxNodes[i].checked) {
                        count++;
                    }
                }
                document.querySelector('table[class|=webapp]>thead>tr>td>div>div>font.count').innerText = count;
                const barNode = document.querySelector('table[class|=webapp]>thead>tr>td>div.webapp-bar.merge');
                if (node.dataset.hide == false) {
                    return;
                }

                if (count > 0) {
                    barNode.style.display = 'flex';
                    barNode.parentNode.style.display = 'table-cell'
                } else {
                    barNode.style.display = 'none';
                    barNode.parentNode.style.display = 'none'
                }
            });
        });
        selectAllNode.addEventListener('click', () => {
            for (let i = 0; i < checkboxNodes.length; i++) {
                checkboxNodes[i].click();
            }
        });

        const formNode = document.querySelector('form#batchForm');
        const deleteAllNode = document.querySelector("#deleteAll");
        const getIds = ckboxs => {
            let ids = '';
            for (let i = 0; i < ckboxs.length; i++) {
                if (ckboxs[i].checked) {
                    ids += ckboxs[i].value + ',';
                }
            }
            return ids;
        };

        if (deleteAllNode != null) {
            deleteAllNode.addEventListener('click', () => {
                formNode.action = deleteAllNode.dataset.action;
                const selectedIds = getIds(checkboxNodes);
                if (selectedIds == '') {
                    alert('请选择要删除的项');
                    return;
                }
                document.querySelector('form>input[name=ids]').value = selectedIds;
                if (confirm('是否确认删除？')) {
                    formNode.submit();
                }
            });
        }

        const changeGroupNode = document.querySelector("#changeGroup");
        if (changeGroupNode != null) {
            changeGroupNode.addEventListener('change', () => {
                formNode.action = changeGroupNode.dataset.action;
                document.querySelector('form>input[name=ids]').value = getIds(checkboxNodes);
                document.querySelector('form>select[name=group_id]').value = document.querySelector('form>select[name=group_id]>option:checked').value;
                if (confirm('是否确认转移？')) {
                    formNode.submit();
                } else {
                    changeGroupNode.value = '';
                }
            });
        }
    }




    const actionSelectNode = document.querySelector("#actionSelect");
    if (actionSelectNode != null) {
        actionSelectNode.addEventListener('change', () => {
            changeBlock(actionSelectNode.value)
        });
    }

    function changeBlock(action) {
        const timeNode = document.querySelector("input[name=times]").parentNode,
            commentAreaNode = document.querySelector("textarea[name=content]");
        if (action == 'comment') {
            timeNode.style.display = 'none';
            commentAreaNode.parentNode.style.display = 'block';
            commentAreaNode.setAttribute("required", "required");
        } else {
            timeNode.style.display = 'block';
            commentAreaNode.parentNode.style.display = 'none';
            commentAreaNode.removeAttribute("required");
        }
    }
    if (actionSelectNode) changeBlock(actionSelectNode.value);

})
